import 'dart:async';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_bmfmap_example/componets/glass_widget.dart';
import 'package:flutter_bmfmap_example/http/http.dart';
import 'package:flutter_bmfmap_example/models/familyinfomodel.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:glassmorphism/glassmorphism.dart';
import 'package:sleek_circular_slider/sleek_circular_slider.dart';
import 'package:flutter_baidu_mapapi_map/flutter_baidu_mapapi_map.dart';
import 'package:flutter_bmfmap_example/tool/tool.dart';
import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart';
import 'package:provider/provider.dart';
import 'package:qrscan/qrscan.dart' as scanner;

class GlassmorphicSample extends StatefulWidget {
  FamilyItemmodel personinfo;
  GlassmorphicSample({this.personinfo, this.index});
  int index;
  @override
  State<GlassmorphicSample> createState() => GlassmorphicSampleState();
}

class GlassmorphicSampleState extends State<GlassmorphicSample> {
  BMFMapController myMapController;

  BMFMapOptions mapOptions = BMFMapOptions(
      compassEnabled: true,
      compassPosition: BMFPoint(50.0, 800.0),
      mapZoomControlPosition: BMFPoint(50.0, 1500.0),
      center: BMFCoordinate(25.0572, 102.74129),
      zoomLevel: 18,
      mapPadding: BMFEdgeInsets(left: 0, top: 0, right: 0, bottom: 0));

  double _lastMoveY = 0;
  double _downY = 0;
  double detal = 0;
  double bottom = -260;
  bool isup = true;
  Timer _timer;

  @override
  void dispose() {
    // TODO: implement dispose
    if (mounted) {
      _timer.cancel();
    }
    super.dispose();
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    const timeout = const Duration(seconds: 1);

    _timer = Timer.periodic(timeout, (timer) {
      updatedata();
    });
  }

  void updatedata() {
    context.read<FamilyInfomodel>().init();
  }

  @override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;

    return Scaffold(
      body: Container(
        height: double.infinity,
        width: double.infinity,
        child: Stack(
          children: [
            Container(
              width: width,
              decoration: new BoxDecoration(
                color: Color.fromRGBO(114, 92, 201, 1),
                //设置四周圆角 角度
                borderRadius: BorderRadius.all(Radius.circular(20.0)),
              ),
              child: ClipRRect(
                borderRadius: BorderRadius.circular(20),
                child: BMFMapWidget(
                  onBMFMapCreated: (controller) {
                    myMapController = controller;
                  },
                  mapOptions: mapOptions,
                ),
              ),
            ),

            Positioned(
              top: 10 + MediaQuery.of(context).padding.top,
              left: 10,
              child: InkWell(
                onTap: () {
                  Navigator.pop(context);
                },
                child: Container(
                  height: 40,
                  width: 40,
                  child: Icon(Icons.arrow_back_outlined),
                  // color: Colors.red,
                  decoration: new BoxDecoration(
                      color: Color.fromRGBO(242, 242, 249, 1),
                      borderRadius: new BorderRadius.circular((12.0)),
                      boxShadow: [
                        BoxShadow(
                            color: Colors.black12,
                            offset: Offset(0.0, 0.0), //阴影xy轴偏移量
                            blurRadius: 5.0, //阴影模糊程度
                            spreadRadius: 2 //阴影扩散程度
                            )
                      ]),
                ),
              ),
            ),

            Positioned(
              top: 10 + MediaQuery.of(context).padding.top,
              right: 10,
              child: InkWell(
                onTap: () {
                  Navigator.pop(context);
                },
                child: Container(
                  height: 40,
                  width: 40,
                  child: PopupMenuButton<int>(
                    onSelected: (int result) {
                      if (result == 1) {
                        scan();
                      }
                      setState(() {});
                    },
                    icon: Icon(Icons.more_horiz),
                    itemBuilder: (BuildContext context) =>
                        <PopupMenuEntry<int>>[
                      const PopupMenuItem<int>(
                        value: 1,
                        child: Text('扫码绑定'),
                      ),
                      // const PopupMenuItem<int>(
                      //   value: 2,
                      //   child: Text('Being a lot smarter'),
                      // ),
                      // const PopupMenuItem<int>(
                      //   value: 3,
                      //   child: Text('Being a self-starter'),
                      // ),
                      // const PopupMenuItem<int>(
                      //   value: 4,
                      //   child: Text('Placed in charge of trading charter'),
                      // ),
                    ],
                  ),
                  // color: Colors.red,
                  decoration: new BoxDecoration(
                      color: Color.fromRGBO(242, 242, 249, 1),
                      borderRadius: new BorderRadius.circular((12.0)),
                      boxShadow: [
                        BoxShadow(
                            color: Colors.black12,
                            offset: Offset(0.0, 0.0), //阴影xy轴偏移量
                            blurRadius: 5.0, //阴影模糊程度
                            spreadRadius: 2 //阴影扩散程度
                            )
                      ]),
                ),
              ),
            ),

            // Image.asset(
            //   'assets/bg.jpg',
            //   fit: BoxFit.cover,
            //   height: double.infinity,
            //   width: double.infinity,
            //   scale: 1,
            // ),

            Positioned(
              bottom: bottom,
              child: Listener(
                onPointerMove: (event) {
                  if (mounted) {
                    var position = event.position.distance;
                    //判断距离差
                    detal = position - _lastMoveY;
                    //  print(detal);
                    if (detal > 0) {
                      isup = false;
                      //手指移动的距离
                      // double pos = (position - _downY);
                      if (bottom > -260) bottom -= 2;
                      // print("================向下移动================");
                    } else {
                      isup = true;
                      if (bottom < 0) bottom += 2;
                      // 所摸点长度 +滑动距离  = IistView的长度  说明到达底部
                      // print("================向上移动================");
                    }
                    _lastMoveY = position;
                  }
                  setState(() {});
                },
                onPointerUp: (event) {
                  var position = event.position.distance;
                  _lastMoveY = position;

                  if (isup) {
                    bottom = 0;
                  } else {
                    bottom = -260;
                  }
                  setState(() {});
                },
                onPointerDown: (PointerDownEvent event) {
                  // print('down');
                },
                child: ClipRRect(
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(24),
                    topRight: Radius.circular(24),
                  ),
                  child: Container(
                    height: 408,
                    child: Stack(
                      children: [
                        Image.asset('assets/images/common/bg1.JPG',
                            width: width, fit: BoxFit.cover),
                        Container(
                          color: Colors.white12,
                          width: width,

                          // linearGradientBeginOpacity: 0.3,
                          // linearGradientEndOpacity: 0.2,
                          // borderGradientBeginOpacity: 0.5,
                          // borderGradientEndOpacity: 0.02,
                          child: Padding(
                            padding: const EdgeInsets.all(16.0),
                            child: Column(
                              mainAxisSize: MainAxisSize.max,
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: [
                                Padding(
                                  padding: const EdgeInsets.only(
                                      left: 100, right: 100, bottom: 16),
                                  child: Container(
                                    height: 5,
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(25),
                                      color: Colors.black87,
                                    ),
                                  ),
                                ),
                                buildUser(),
                                SizedBox(
                                  height: 8,
                                ),
                                buildInfo(),
                                // SizedBox(
                                //   height: 30,
                                // ),
                                // buildProgress(),
                                buildMessage(),

                                Padding(
                                  padding: const EdgeInsets.all(8.0),
                                  child: GlassWidget(
                                    height: 44,
                                    width: 120,
                                    borderRadius: 10,
                                    blur: 10,
                                    linearGradientBeginOpacity: 0.4,
                                    linearGradientEndOpacity: 0.4,
                                    borderGradientBeginOpacity: 0.5,
                                    borderGradientEndOpacity: 0.1,
                                    child: Center(
                                      child: Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceEvenly,
                                        crossAxisAlignment:
                                            CrossAxisAlignment.center,
                                        children: [
                                          SvgPicture.asset(
                                            'assets/images/icons/svg/视频通话.svg',
                                            width: 36,
                                          ),
                                          Text('视频通话'),
                                        ],
                                      ),
                                    ),
                                  ),
                                ),
                                // buildWork(),
                                // buildBottom(),
                              ],
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
            // SafeArea(
            //   child: Center(
            //       // child:
            //       ),
            // ),
          ],
        ),
      ),
    );
  }

  Widget buildUser() => Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          buildName(),
          buildPic(),
        ],
      );
  Widget buildName() => Container(
        padding: EdgeInsets.only(left: 15),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              widget.personinfo.personName,
              style: TextStyle(
                color: Color(0xFFeb3c63),
                fontSize: 30,
                letterSpacing: 1,
                fontWeight: FontWeight.w700,
              ),
            ),
            Text(
              widget.personinfo.relationId.toString(),
              style: TextStyle(
                color: Color(0xFFef7079),
                fontSize: 20,
                letterSpacing: 1,
                fontWeight: FontWeight.w500,
              ),
            )
          ],
        ),
      );

  Widget buildPic() => Hero(
        tag: widget.personinfo.logoId,
        child: Padding(
          padding: const EdgeInsets.only(right: 15),
          child: Container(
            padding: EdgeInsets.all(5),
            height: 100,
            width: 100,
            // decoration: BoxDecoration(
            //   borderRadius: BorderRadius.circular(25),
            //   gradient: LinearGradient(
            //       begin: Alignment.topLeft,
            //       end: Alignment.bottomRight,
            //       colors: [
            //         Color(0xFFec445b).withOpacity(0.8),
            //         Color(0xFFfbb055).withOpacity(0.8)
            //       ]),
            // ),
            child: Container(
              width: 90,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(20),
                // image: DecorationImage(
                //   fit: BoxFit.cover,
                //   image: Image.asset('name'),
                // ),
              ),
              child: Image.asset(Tool.getportrait(widget.personinfo.logoId)),
            ),
          ),
        ),
      );

  Widget buildInfo() => Container(
        child: Row(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            buildInfoItem('身高', widget.personinfo.height.toString()),
            buildInfoItem('体重', widget.personinfo.weight.toString()),
            buildInfoItem('大卡', widget.personinfo.personBirth)
          ],
        ),
      );

  Widget buildInfoItem(String text1, String text2) => Center(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: GlassWidget(
            width: 100,
            height: 44,
            borderRadius: 5,
            blur: 10,
            linearGradientBeginOpacity: 0.4,
            linearGradientEndOpacity: 0.4,
            borderGradientBeginOpacity: 0.5,
            borderGradientEndOpacity: 0.1,
            child: Center(
              child: RichText(
                text: TextSpan(
                  text: text1,
                  style: TextStyle(
                    color: Color(0xff757575),
                    fontSize: 16,
                    fontWeight: FontWeight.normal,
                  ),
                  children: [
                    TextSpan(
                      text: text2,
                      style: TextStyle(
                        color: Color(0xFFe82c5c),
                        fontSize: 18,
                        fontWeight: FontWeight.bold,
                      ),
                    )
                  ],
                ),
              ),
            ),
          ),
        ),
      );

  Widget buildProgress() => SleekCircularSlider(
        innerWidget: (double value) {
          return Center(
            child: buildCenterInfo(),
          );
        },
        appearance: CircularSliderAppearance(
          startAngle: -80,
          angleRange: 400,
          size: 250,
          customColors: CustomSliderColors(
            trackColor: Color(0xffffffff).withOpacity(0.6),
            progressBarColors: [
              Color(0xfffec753),
              Color(0xfff16d58),
            ],
            shadowColor: Color(0xffffffff).withOpacity(0.1),
          ),
          customWidths: CustomSliderWidths(
            progressBarWidth: 30,
            trackWidth: 30,
          ),
        ),
      );

  Widget buildCenterInfo() => GlassWidget(
        width: 130,
        height: 130,
        borderRadius: 65,
        blur: 10,
        linearGradientBeginOpacity: 0.6,
        linearGradientEndOpacity: 0.4,
        borderGradientBeginOpacity: 0.2,
        borderGradientEndOpacity: 0.05,
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                '6550',
                style: TextStyle(
                  color: Color(0xfff26a5a).withOpacity(0.9),
                  fontSize: 30,
                  letterSpacing: -2,
                  fontWeight: FontWeight.w700,
                ),
              ),
              Text(
                '步数',
                style: TextStyle(
                  color: Color(0xfff79056).withOpacity(0.9),
                  fontSize: 15,
                  letterSpacing: -2,
                  fontWeight: FontWeight.w700,
                ),
              ),
            ],
          ),
        ),
      );

  Widget buildMessage() => Container(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          mainAxisSize: MainAxisSize.max,
          children: [
            buildMessageItem(
                'assets/heart.png',
                '心率',
                context
                    .watch<FamilyInfomodel>()
                    .familylist[widget.index]
                    .rateNow
                    .toString()),
            buildMessageItem(
                'assets/water.png', '体温', widget.personinfo.temp.toString()),
            buildMessageItem(
                'assets/moon.png', '步数', widget.personinfo.steps.toString())
          ],
        ),
      );
  Widget buildMessageItem(String image, String text, String content) => Padding(
        padding: const EdgeInsets.all(8.0),
        child: GlassWidget(
            width: 100,
            height: 108,
            borderRadius: 10,
            blur: 10,
            linearGradientBeginOpacity: 0.4,
            linearGradientEndOpacity: 0.4,
            borderGradientBeginOpacity: 0.5,
            borderGradientEndOpacity: 0.1,
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Image.asset(
                    image,
                    height: 40,
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  Container(
                    width: 80,
                    child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            text,
                            style: TextStyle(
                                color: Color(0xff535353),
                                fontSize: 16,
                                fontWeight: FontWeight.bold),
                          ),
                          Text(
                            content,
                            style: TextStyle(
                                color: Color(0xff535353),
                                fontSize: 16,
                                fontWeight: FontWeight.bold),
                          ),
                        ]),
                  ),
                ],
              ),
            )),
      );
  Widget buildWork() => Container(
        padding: EdgeInsets.only(top: 7),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            buildWorkItem('assets/workout.png', '健身日程'),
            buildWorkItem('assets/diet.png', '健康饮食')
          ],
        ),
      );
  Widget buildWorkItem(String image, String text) => Center(
        child: GlassWidget(
          width: 168,
          height: 60,
          borderRadius: 10,
          blur: 10,
          linearGradientBeginOpacity: 0.4,
          linearGradientEndOpacity: 0.4,
          borderGradientBeginOpacity: 0.5,
          borderGradientEndOpacity: 0.1,
          child: Center(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Image.asset(
                  image,
                  height: 40,
                ),
                SizedBox(
                  width: 10,
                ),
                Text(
                  text,
                  style: TextStyle(
                      color: Color(0xff535353),
                      fontSize: 16,
                      fontWeight: FontWeight.bold),
                ),
              ],
            ),
          ),
        ),
      );
  Widget buildBottom() => Container(
        margin: EdgeInsets.only(top: 40),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            IconButton(
              icon: Icon(
                Icons.calendar_today,
                color: Color(0xffe82c5c),
              ),
              onPressed: null,
            ),
            IconButton(
              icon: Icon(Icons.person_outline_outlined),
              onPressed: null,
            ),
            IconButton(
              icon: Icon(Icons.settings_outlined),
              onPressed: null,
            ),
            IconButton(
              icon: Icon(Icons.file_present),
              onPressed: null,
            )
          ],
        ),
      );

  //扫码函数,最简单的那种
Future scan() async {
  String cameraScanResult = await scanner.scan(); //通过扫码获取二维码中的数据
  // cameraScanResult = "";
  print(cameraScanResult);
  
  // getScan(cameraScanResult); //将获取到的参数通过HTTP请求发送到服务器>
  // print(cameraScanResult); //在控制台打印
}

//get请求，用于验证数据(也可以在控制台直接打印，但模拟器体验不好)
void getScan(String scan) async {
  // var url="http://164.155.66.12/GetScan?scan="+scan;
  // var dio = Dio();
  // var response = await dio.get(url);
  // var data = response.data.toString();
  // print(data);
  Http.get('/linkPerson/addApp');
}

}
